package com.easy.cloud.web.service.upms.biz.controller;

import com.easy.cloud.web.component.core.response.HttpResult;
import com.easy.cloud.web.component.log.annotation.SysLog;
import com.easy.cloud.web.component.log.annotation.SysLog.Action;
import com.easy.cloud.web.service.upms.api.dto.RoleDTO;
import com.easy.cloud.web.service.upms.api.dto.RoleQueryDTO;
import com.easy.cloud.web.service.upms.api.vo.RoleVO;
import com.easy.cloud.web.service.upms.biz.service.IRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * Role API
 *
 * @author Fast Java
 * @date 2023-08-03 14:32:52
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping(value = "role")
@Api(value = "角色管理", tags = "角色管理")
public class RoleController {

    private final IRoleService roleService;

    /**
     * 新增
     *
     * @param roleDTO 新增数据
     * @return 新增数据
     */
    @PostMapping(value = "save")
    @PreAuthorize("@pms.hasPermission('system:role:add')")
    @SysLog(value = "角色新增", action = Action.ADD)
    @ApiOperation(value = "角色新增")
    public HttpResult<RoleVO> save(@Validated @RequestBody RoleDTO roleDTO) {
        return HttpResult.ok(roleService.save(roleDTO));
    }

    /**
     * 更新
     *
     * @param roleDTO 新增数据
     * @return 更新数据
     */
    @PostMapping(value = "update")
    @PreAuthorize("@pms.hasPermission('system:role:update')")
    @SysLog(value = "角色更新", action = Action.UPDATE)
    @ApiOperation(value = "角色更新")
    public HttpResult<RoleVO> update(@Validated @RequestBody RoleDTO roleDTO) {
        return HttpResult.ok(roleService.update(roleDTO));
    }

    /**
     * 根据ID移除数据
     *
     * @param roleId ID
     * @return 是否删除成功
     */
    @GetMapping(value = "remove/{roleId}")
    @PreAuthorize("@pms.hasPermission('system:role:delete')")
    @SysLog(value = "角色删除", action = Action.DELETE)
    @ApiOperation(value = "角色删除")
    public HttpResult<Boolean> removeById(@PathVariable @NotBlank(message = "当前ID不能为空") String roleId) {
        return HttpResult.ok(roleService.removeById(roleId));
    }

    /**
     * 根据ID获取详情
     *
     * @param roleId ID
     * @return 详情数据
     */
    @GetMapping(value = "detail/{roleId}")
    @PreAuthorize("@pms.hasPermission('system:role:query')")
    @SysLog(value = "角色详情", action = Action.FIND)
    @ApiOperation(value = "角色详情")
    public HttpResult<RoleVO> detailById(@PathVariable @NotBlank(message = "当前ID不能为空") String roleId) {
        return HttpResult.ok(roleService.detailById(roleId));
    }

    /**
     * @param roleQueryDTO 查询参数
     * @return 查询列表
     */
    @GetMapping(value = "list")
    @PreAuthorize("@pms.hasPermission('system:role:query')")
    @SysLog(value = "角色列表", action = Action.FIND)
    @ApiOperation(value = "角色列表")
    public HttpResult<List<RoleVO>> list(RoleQueryDTO roleQueryDTO) {
        return HttpResult.ok(roleService.list(roleQueryDTO));
    }

    /**
     * @param roleQueryDTO 查询参数
     * @return 查询分页数据
     */
    @GetMapping(value = "page")
    @PreAuthorize("@pms.hasPermission('system:role:query')")
    @SysLog(value = "角色分页", action = Action.FIND)
    @ApiOperation(value = "角色分页")
    public HttpResult<Page<RoleVO>> page(RoleQueryDTO roleQueryDTO) {
        return HttpResult.ok(roleService.page(roleQueryDTO));
    }
}